﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Cms.Render;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Cms.Bussiness;
using Portal.Cms.Model;
using Portal.Web.Utility;
using Portal.Cms.Utility;

namespace Portal.Cms.Render
{
    public class SelectDataRenderByTreeBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                string folder = (string)WebEnvironments.Request(SiteId, "folder");
                string GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "GroupDataFolderId");
                if (string.IsNullOrEmpty(GroupDataFolderId)) GroupDataFolderId = "";
                string Html =
                    DrawFunction(SiteId, gid).HtmlContent +
                    "<script language=javascript>\r\n"+
                    "   function ClientSelectDocument(DataId)\r\n"+
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       SelectedGroupDataFolderId=document.getElementById('txtSelectedGroupDataFolderId').value;\r\n" +
                    "       AjaxOut = Portal.Cms.Render.SelectDataRenderByTreeBll.ServerSideAddDocumentToPublicFolder(SiteId,'" + gid + "','" + folder + "','+SelectedGroupDataFolderId+',DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('divDocumentList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       alert(AjaxOut.InfoMessage);\r\n"+
                    "   }\r\n"+
                    "</script>\r\n"+
                    "<div class=CssFormData>\r\n" +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1><div class=CssDivFolder>" + DrawFolder(SiteId, gid, folder).HtmlContent + "</div></td>\r\n" +
                    "       <td class=col2>\r\n"+
                    "           <h3>Danh sách văn bản trong thư mục<input type=button class=button style=\"float:right;margin-right:2px;\" value=\"Điều kiện tìm kiếm\"></h3>\r\n"+
                    "           <div class=CssDocumentList id=\"divDocumentList\">" +
                                       ServerSideReadingDocumentInFolder(SiteId, gid, folder, GroupDataFolderId).HtmlContent + "\r\n" +
                    "           </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideAddDocumentToPublicFolder(
        //    string SiteId, 
        //    string GroupDataId, 
        //    string folder,
        //    string GroupDataFolderId,
        //    string DataId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddDataToPublicGroupDataFolder(SiteId, new string[] { folder}, DataId);
        //        OAjaxOut.RetUrl = "PublicDocumentInputDataTreeView.aspx?gid="+GroupDataId+"&folder="+folder;
        //        OAjaxOut.InfoMessage = "Đưa vào nhóm thành công";
        //        OAjaxOut.HtmlContent = ServerSideReadingDocumentInFolder(SiteId, GroupDataId, folder, GroupDataFolderId).HtmlContent;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut DrawFolder(string SiteId, string GroupDataId, string folder)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html =
        //                      "<script language=javascript>\r\n"+
        //                      "     function ClientLoadDocument(GroupDataId,GroupDataFolderId)\r\n"+
        //                      "     {\r\n"+
        //                      "         SiteId='"+SiteId+"';\r\n"+
        //                      "         folder='"+folder+"';\r\n"+
        //                      "         document.getElementById('txtSelectedGroupDataFolderId').value = GroupDataFolderId;\r\n" +
        //                      "         AjaxOut = Portal.Cms.Render.SelectDataRenderByTreeBll.ServerSideReadingDocumentInFolder(SiteId,GroupDataId,folder, GroupDataFolderId).value;\r\n" +
        //                      "         document.getElementById('divDocumentList').innerHTML=AjaxOut.HtmlContent;\r\n"+
        //                      "     }\r\n"+
        //                      "</script>\r\n"+
        //                      "<input type=hidden id=\"txtSelectedGroupDataFolderId\" value=\"\">\r\n" +
        //                      "<h3><a href=\"DocumentInputDataTreeView.aspx?gid="+GroupDataId+"\">Thư mục dữ liệu</a></h3>\r\n" +
        //                      "<div id=\"tree\">\r\n"+
        //                      "<ul>\r\n";
        //        GroupDataFolderCls[] GroupDataFolders=GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId,GroupDataId,null);
        //        for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
        //        {
        //            Html +=
        //                    "<li class=\"folder expanded\"><a href=\"" + GroupDataId + "|" + GroupDataFolders[iIndex].GroupDataFolderId + "\" target=\"_self\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
        //                        BuildSubTreeView(SiteId,GroupDataId,GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent+
        //                    "</li>\r\n";
        //        }

        //        Html += "</ul>\r\n" +
        //                "</div>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut BuildSubTreeView(string SiteId, string GroupDataId,string ParentGroupDataFolderId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "";
        //        GroupDataFolderCls[] GroupDataFolders = GroupDataFolderProcessBll.ReadingGroupDataFolder(SiteId, GroupDataId, ParentGroupDataFolderId);
        //        Html+=
        //            "<ul>\r\n";
        //        for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
        //        {
        //            Html +=
        //                    "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupDataId + "|" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
        //                        BuildSubTreeView(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
        //                    "</li>\r\n";
        //        }

        //        Html += "</ul>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFunction(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html =
                   "<script language=javascript>\r\n"+
                   
                   "</script>\r\n"+
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:SelectAllFolder('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/SelectedFolder.png\"><div>Chọn thư mục</div></li>\r\n" +
                   "            <li onclick=\"javascript:SelectSelectedDocument('" + GroupDataId + "');\"><img class=image src=\"Resources/Images/SelectedDocument.png\"><div>Chọn văn bản đánh dấu</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('home.aspx','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

       
        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideReadingDocumentInFolder(
        //    string SiteId, 
        //    string GroupDataId, 
        //    string folder,
        //    string GroupDataFolderId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        string Html = "";
        //        GroupDataFolderCls OGroupDataFolder = null;
        //        if (!string.IsNullOrEmpty(GroupDataFolderId))
        //        {
        //            OGroupDataFolder = GroupDataFolderProcessBll.CreateGroupDataFolderModel(SiteId, GroupDataFolderId);
        //        }
                
        //        if (OGroupDataFolder != null)
        //        {
        //            Html +=
        //                "<h3>Đang chọn thư mục: " + OGroupDataFolder.FolderName + "</h3>";
        //        }
        //        CmsDataCls[] CmsDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataForFolderForAddPublicFolder(SiteId, GroupDataId, GroupDataFolderId, folder);
        //        if (CmsDatas.Length == 0)
        //        {
        //            Html += "<div class=CssNoData>Không có văn bản nào thuộc thư mục</div>\r\n";
        //        }
        //        else
        //        {
        //            Html += "<div class=tabcontent id=divListDocResult>\r\n" +
        //                   "    <table>\r\n" +
        //                   "        <tr>\r\n" +
        //                   "            <th class=thIndex>#</th>\r\n" +
        //                   "            <th>Tiêu đề văn bản</th>\r\n" +
        //                   "            <th>Kiểu</th>\r\n" +
        //                   "            <th></th>\r\n" +
        //                   "        </tr>\r\n";
        //            for (int iIndex = 0; iIndex < CmsDatas.Length; iIndex++)
        //            {
        //                Html+=
        //                   "        <tr>\r\n" +
        //                   "            <td class=tdIndex>"+(iIndex+1)+"</td>\r\n" +
        //                   "            <td><span class=CssDocumentSubject><a href=\"ViewDocument.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolderId + "&DataId=" + CmsDatas[iIndex].DataId + "\">" + CmsDatas[iIndex].Subject + "</a></span><br><span class=CssDocDesc>" + FunctionUtilities.CutText(CmsDatas[iIndex].Description, 50) + "</span></td>\r\n" +
        //                   "            <td>" + CmsDatas[iIndex].SubDataType + "</td>\r\n" +
        //                   "            <td class=tdButton><img onclick=\"javascript:ClientSelectDocument('" + CmsDatas[iIndex].DataId + "');\" alt=\"Chọn văn bản này\" title=\"Chọn văn bản này\" class=imagebutton src=\"Resources/Images/Download.png\"></td>\r\n" +
        //                   "        </tr>\r\n";
        //            }
        //            Html += "</table>\r\n" +
        //                    "</div>\r\n";
        //        }
        //        OAjaxOut.HtmlContent = Html;
                    
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}
    }
}
